package com.tgpms.web.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.interfaces.dto.VformInterface;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import javax.annotation.Resource;

/**
 * @author Jeff
 * <p>
 * 公共查询接口调用
 */
@Api(value = "ComInterfaceController", tags = "公共查询接口调用")
@RestController
@RequestMapping(value = "/ev/comInterface")
public class ComInterfaceController {

    @Resource
    private VformInterface vformInterface;
    
    @ApiOperation(value = "VForm 8.0接口： 屏幕查询接口", notes = "VForm 8.0接口： 屏幕查询接口", httpMethod = "POST")
    @PostMapping(value = "/progCodeFind")
    public Result progCodeFind(@RequestBody(required = false) Map<String, String> map) {
        Result result = null;
        try {
        	String progCode = map.get("progCode");
        	map.remove("progCode");
        	String pageNo = map.get("pageNo");
        	map.remove("pageNo");
        	String pageSize = map.get("pageSize");
        	map.remove("pageSize");
            String dataOwner = map.get("dataowner");
            map.remove("dataowner");
        	JSONObject json = new JSONObject();
        	for(String s : map.keySet()) {
        		json.put(s, map.get(s));
        	}
            System.out.println("上送参数为:"+json.toString());
            if(dataOwner==null) {
            	result = vformInterface.find(progCode,json.get("data").toString(),pageNo,pageSize,"N","N","");
            }else {
            	result = vformInterface.find(progCode,json.get("data").toString(),pageNo,pageSize,"Y","Y",dataOwner);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @ApiOperation(value = "VForm 12.1接口： 特殊sql查询接口 操作", notes = "VForm 12.1接口： 特殊sql查询接口 操作", httpMethod = "POST")
    @PostMapping(value = "/specialSQL")
    public Result specialSQL(@RequestParam(value = "code", required = true) String code,
                             @RequestParam(value = "selparam", required = false) String selparam,
                             @RequestParam(value = "leftdition", required = false) String leftdition,
                             @RequestParam(value = "condition", required = false) String condition,
                             @RequestParam(value = "dataowner", required = false) String dataowner,
                             @RequestParam(value = "pageNo", required = true,defaultValue = "1") String pageNo,
                             @RequestParam(value = "pageSize", required = true,defaultValue = "10") String pageSize) {
        Result result = null;
        try {
            JSONObject json = new JSONObject();
            if(selparam==null) {
            	json.put("[SQL1]", "");
            }else {
            	json.put("[SQL1]", selparam);
            }
            if(leftdition==null) {
            	json.put("[SQL2]", "");
            }else {
            	json.put("[SQL2]", leftdition);
            }
            if(condition==null) {
            	json.put("[SQL3]", "");
            }else {
            	json.put("[SQL3]", condition);
            }
            System.out.println("上送参数为:"+json.toJSONString());
            if (StringUtils.isEmpty(dataowner))
                result = vformInterface.multiForMsQueryBySql(code, json.toJSONString(),pageNo, pageSize, "N", "N", null);
            else
                result = vformInterface.multiForMsQueryBySql(code, json.toJSONString(),pageNo, pageSize, "Y", "Y", dataowner);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    @ApiOperation(value = "VForm 15接口：报表下载操作", notes = "VForm 15接口： 报表下载 操作", httpMethod = "POST")
    @PostMapping(value = "/runReport")
    public Result runReport(@RequestParam(value = "param", required = true) String param  ,
                            @RequestParam(value = "reportCode", required = true) String reportCode,
                            @RequestParam(value = "desformat", required = false) String desformat) {
        Result result = new Result();
        try {
            result = vformInterface.runReport(param,reportCode,desformat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @ApiOperation(value = "VForm 16接口：旧报表打印接口", notes = "VForm 16接口：旧报表打印接口", httpMethod = "POST")
    @PostMapping(value = "/runReportForOld")
    public Result runReportForOld(@RequestParam(value = "param", required = true) String param  ,
                            @RequestParam(value = "reportCode", required = true) String reportCode) {
        Result result = new Result();
        try {
            result = vformInterface.runReportForOld(param,reportCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
